/**
 * TempDisplay.java extends class IOIOActivity from ioio.lib.util.android
 * It contains a class called IOIOLooper which implements a similar named class in ioio.lib.util
 * The Overridden methods appearing in that class are implemented automatically
 * Further comments can be found next to the targeted code
 */

package com.example.smartpillow;

import ioio.lib.api.AnalogInput;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.IOIO;
import ioio.lib.api.exception.ConnectionLostException;

import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.android.IOIOActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class TempDisplay extends IOIOActivity{
	
	private TextView tempDisplay1;
	private TextView tempDisplay2;
	private TextView tempDisplay3;
	private TextView tempDisplay4;
	private TextView tempDisplay5;
	private TextView tempDisplay6;
	private ToggleButton button_;
	private float maxTemptemp = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.temp_display_normal); //crashes if set to activity_temp_display. I think its because at the moment its not reading any temperature, left as normal view 
		
		tempDisplay1 = (TextView) findViewById(R.id.textView1);
		tempDisplay2 = (TextView) findViewById(R.id.alarmview);

		button_ = (ToggleButton) findViewById(R.id.toggleButton1);
		ImageButton goBack = (ImageButton) findViewById(R.id.imageButton2);
		goBack.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				
				finish(); //finish() is used to kill the current activity and revert back to main page
				
				//this will eventually be changed to keeping current activity in the background for overnight monitoring purposes
				
			}
			
		});
		
    }    
        class Looper extends BaseIOIOLooper{
        	
        	private DigitalOutput led_;
        	private AnalogInput tempFromPin1_;
        	private AnalogInput tempFromPin2_;
        	private AnalogInput tempFromPin3_;
        	private AnalogInput tempFromPin4_;
        	private AnalogInput tempFromPin5_;
        	private int boardPin1=41;
        	private int boardPin2=42;
        	private int boardPin3=43;
        	private int boardPin4=44;
        	private int boardPin5=45;
        	private float getVoltage1;
        	private float getVoltage2;
        	private float getVoltage3;
        	private float getVoltage4;
        	private float getVoltage5;
        	public  float temp1 = (float)0.00;
        	public  float temp2 = (float)0.00;
        	public  float temp3 = (float)0.00;
        	public  float temp4 = (float)0.00;
        	public  float temp5 = (float)0.00;
        	@Override
        	protected void setup() throws ConnectionLostException{
        		led_ = ioio_.openDigitalOutput(0, true);
        		tempFromPin1_=ioio_.openAnalogInput(boardPin1);
        		tempFromPin2_=ioio_.openAnalogInput(boardPin2);
        		tempFromPin3_=ioio_.openAnalogInput(boardPin3);
        		tempFromPin4_=ioio_.openAnalogInput(boardPin4);
        		tempFromPin5_=ioio_.openAnalogInput(boardPin5);
        	}
        	
        	@Override
			public void loop() throws ConnectionLostException{
        		led_.write(!button_.isChecked());
        		
        		try{
        			Thread.sleep(5000);
        			getVoltage1=tempFromPin1_.getVoltage();
        			System.out.println(getVoltage1);
        			getVoltage2=tempFromPin2_.getVoltage();
        			getVoltage3=tempFromPin3_.getVoltage();
        			getVoltage4=tempFromPin4_.getVoltage();
        			getVoltage5=tempFromPin5_.getVoltage();
        		}catch (InterruptedException e){
        			tempFromPin1_.close();
        			tempFromPin2_.close();
        			tempFromPin3_.close();
        			tempFromPin4_.close();
        			tempFromPin5_.close();
        			e.printStackTrace();
        		}
        		temp1=((getVoltage1*1024)-500)/10;
        		temp2=((getVoltage2*1024)-500)/10;
        		temp3=((getVoltage3*1024)-500)/10;
        		temp4=((getVoltage4*1024)-500)/100;
        		temp5=((getVoltage5*1024)-500)/100;
        		
        		float[] templist = new float[5];
        		templist[0]=temp1;
        		templist[1]=temp2;
        		templist[2]=temp3;
        		templist[3]=temp4;
        		templist[4]=temp5;
        		
        		float MaxTemp = templist[0];
        		for (int i=1;i<templist.length;i++){
        			if (MaxTemp<templist[i]){
        				MaxTemp = templist[i];
        			}
        				
        		}
        		maxTemptemp=MaxTemp;
        		
        		runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						
						tempDisplay1.setText(Float.toString(temp1));
						tempDisplay2.setText(Float.toString(temp2));
						tempDisplay3.setText(Float.toString(temp3));
						tempDisplay4.setText(Float.toString(temp4));
						tempDisplay5.setText(Float.toString(temp5));
						// TODO Auto-generated method stub
						
						tempDisplay6.setText(Float.toString(maxTemptemp));
						
					}
				});
        	}
        	
        }	
        @Override
    	protected IOIOLooper createIOIOLooper() {
    		return new Looper();
    	}
        
}